﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>DateDiff - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The DateDiff function compares two date-time values and returns the difference." />
<meta name="ahk:equiv-v1" content="commands/EnvSub.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>DateDiff</h1>

<p>Compares two <a href="FileSetTime.htm#YYYYMMDD">date-time</a> values and returns the difference.</p>

<pre class="Syntax">Result := <span class="func">DateDiff</span>(DateTime1, DateTime2, TimeUnits)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>DateTime1</dt>
  <dt>DateTime2</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>Date-time stamps in the <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format.</p>
      <p>If <em>DateTime1</em> is earlier than <em>DateTime2</em>, the result is a negative number.</p>
  </dd>

  <dt>TimeUnits</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>Units to measure the difference in. <em>TimeUnits</em> may be one of the following strings (or just the first letter): Seconds, Minutes, Hours or Days.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>This function returns the difference between the two timestamps, in the units specified by <em>TimeUnits</em>. The result is always rounded <em>down</em> to the nearest integer. 例如, if the actual difference between two timestamps is 1.999 days, it will be reported as 1 day. If higher precision is needed, specify Seconds for <em>TimeUnits</em> and divide the result by 60.0, 3600.0, or 86400.0.</p>

<h2 id="Remarks">备注</h2>
<p>The built-in variable <strong>A_Now</strong> contains the current local time in <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format.</p>
<p>To precisely determine the elapsed time between two events, use the <a href="../Variables.htm#TickCount">A_TickCount method</a> because it provides millisecond precision.</p>
<p>To add or subtract a certain number of seconds, minutes, hours, or days from a timestamp, use <a href="DateAdd.htm">DateAdd</a> (subtraction is achieved by adding a negative number).</p>
<p>If <em>DateTime</em> contains an invalid timestamp or a year prior to 1601, an exception is thrown.</p>

<h2 id="Related">相关</h2>
<p><a href="DateAdd.htm">DateAdd</a>, <a href="FileGetTime.htm">FileGetTime</a>, <a href="FormatTime.htm">FormatTime</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>var1 := "20050126"
var2 := "20040126"
MsgBox DateDiff(var1, var2, "days")  <em>; The answer will be 366 since 2004 is a leap year.</em>
</pre>
</div>

</body>
</html>